package com.palm.hubo2o.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.palm.hubo2o.BaseActivity;
import com.palm.hubo2o.R;
import com.palm.hubo2o.adapter.PrizeDetailAdapter;
import com.palm.hubo2o.model.Issue;
import com.palm.hubo2o.model.IssuePrize;
import com.palm.hubo2o.network.PalmCallBack;
import com.palm.hubo2o.network.PalmRetrofit;
import com.palm.hubo2o.network.Request;
import com.palm.hubo2o.network.Result;
import com.palm.hubo2o.view.ListItemDecoration;
import com.palm.hubo2o.view.PrizeView;
import com.palm.palmutils.widget.EasyRecyclerView;

import java.text.DecimalFormat;

import butterknife.Bind;
import butterknife.BindColor;
import retrofit2.Call;

/**
 * Created by dest1 on 2015/10/13.
 */
public class PrizeDetailActivity extends BaseActivity {
    @Bind(R.id.header_prize_detail)
    PrizeView prizeView;
    @Bind(R.id.sales_prize_detail)
    TextView tvSales;
    @Bind(R.id.pool_prize_detail)
    TextView tvPool;
    @Bind(R.id.group_balls)
    ViewGroup ballGroup;
    @Bind(R.id.recycler_view)
    EasyRecyclerView mRecyclerView;
    @Bind(R.id.item_header)
    View header;
    @BindColor(R.color.red)
    int red;
    @BindColor(R.color.blue)
    int blue;
    private Issue mIssue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prize_detail);
        mIssue = (Issue) getIntent().getSerializableExtra("issue");
        setTitle(mIssue.gameId.getName() + "开奖");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//        mRecyclerView.addItemDecoration(new (ContextCompat.getDrawable(getContext(), R.drawable.divider_horizontal_dark), true));
        mRecyclerView.addItemDecoration(new ListItemDecoration(2));
        header.setBackgroundColor(Color.GRAY);
        initLotteryInfo();
    }

    private void initLotteryInfo() {
//        tvName.setText(mIssue.gameId.getName());
//        tvNumber.setText(String.format(Locale.getDefault(), "第%d期", mIssue.issueNo));
        prizeView.setName(mIssue.gameId.getName());
        prizeView.setIssue(mIssue.issueNo);
        prizeView.setDate(mIssue.prizeTime);
        prizeView.createBalls(mIssue.prizeBalls.getRed(), mIssue.prizeBalls.getBlue());
        Issue i = new Issue();
        i.gameId = mIssue.gameId;
        i.issueNo = mIssue.issueNo;
        Call<Result<IssuePrize>> call = PalmRetrofit.getInstance().getApi().queryPrizeDetail(new Request<>(i));
        header.setVisibility(View.GONE);
        call.enqueue(new PalmCallBack<IssuePrize>(this) {
            @Override
            public void succeed(IssuePrize issuePrize) {
                DecimalFormat format = new DecimalFormat("#,###");
                tvSales.setText(String.format("本期销量（元）\n%s", format.format(issuePrize.getSaleMoney())));
                tvPool.setText(String.format("奖金池奖金（元）\n%s", format.format(issuePrize.getPrizePool())));
                PrizeDetailAdapter adapter = new PrizeDetailAdapter(issuePrize.getPrizeLevels());
                mRecyclerView.setAdapter(adapter);
                if (adapter.getItemCount() != 0)
                    header.setVisibility(View.VISIBLE);
            }

            @Override
            public void failed(String message) {
                Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
            }
        });

    }
}
